
/**
 * 将Map转换为List(formData转换为table-box所需数组)
 * @param {*} data 需要转换的Map
 */
export function ArrayFormat(data, factorObj, operator = 'like') {
  const params = []
  if (factorObj === undefined || factorObj === 'undefined') factorObj = {}
  if (typeof data === 'object') {
    Object.keys(data).forEach((i) => {
      if (data[i] !== '[]' && data[i] !== '' && data[i] !== undefined && data[i] !== 'undefined' && data[i] !== null) {
        if (Array.isArray(data[i]) && data[i].length === 0) return
        if (factorObj[i]) {
          if (Array.isArray(data[i]) && data[i].length) {
            data[i].forEach((item, index) => {
              if (item) params.push({ id: Math.floor(Math.random() * 0xffffffffff).toString(16), value: item, property: i, operator: factorObj[i][index] })
            })
          } else {
            params.push({ id: Math.floor(Math.random() * 0xffffffffff).toString(16), value: data[i], property: i, operator: factorObj[i] })
          }
          return
        }
        params.push({ id: Math.floor(Math.random() * 0xffffffffff).toString(16), value: data[i], property: i, operator: operator })
      }
    })
  }
  return params
}

/**
 * Map去掉空值(formData转换为table-box所需对象)
 * @param {*} data 需转换的Map
 */
export function ObjFormat(data) {
  const params = {}
  if (typeof data === 'object') {
    Object.keys(data).forEach((i) => {
      if (data[i] !== '' && data[i] !== undefined && data[i] !== 'undefined') {
        if (Array.isArray(data[i]) && data[i].length === 0) return
        params[i] = data[i]
      }
    })
  }
  return params
}

export function JsonToUrl(json) {
  const params = []
  if (typeof json === 'object') {
    Object.keys(json).forEach((key) => {
      if (json[key] !== '' && !json[key] !== undefined && json[key] !== 'undefined') {
        // const str = `${encodeURIComponent(key)}=${encodeURIComponent(json[key])}`
        const str = `${key}=${json[key]}`
        params.push(str)
      }
    })
  }
  if (params.length) {
    let str = params.join('&')
    str = `?${str}`
    return str
  }
  return ''
}
